package com.ZeroCarbon.common_service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 服务层通用方法
 *
 * @since 2025-2-14
 */
public class CommonService<T> {

    private final BaseMapper<T> mapper;

    @Autowired
    public CommonService(BaseMapper<T> mapper) {
        this.mapper = mapper;
    }

    /**
     * SQL自增自减
     */
    public Boolean updateSqlIncrementOrCheck(String field, int step, String id) {
        //更新构造器
        UpdateWrapper<T> updateWrapper = new UpdateWrapper<>();
        //更新语句
        updateWrapper.setSql(field + " = " + field + " + " + step)
                .eq("id", id);
        //更新信息
        return mapper.update(null, updateWrapper) > 0;
    }
}
